package com.ruoyi.project.jiaqin.downquotation.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.mapper.CompanyMapper;
import com.ruoyi.project.jiaqin.downclass.domain.DownClass;
import com.ruoyi.project.jiaqin.downclass.mapper.DownClassMapper;
import com.ruoyi.project.jiaqin.downmeasure.domain.DownMeasure;
import com.ruoyi.project.jiaqin.downmeasure.mapper.DownMeasureMapper;
import com.ruoyi.project.jiaqin.downquotation.domain.DownQuotation;
import com.ruoyi.project.jiaqin.downquotation.mapper.DownQuotationMapper;
import com.ruoyi.project.jiaqin.downquotation.service.IDownQuotationService;
import com.ruoyi.project.jiaqin.downstandard.domain.DownStandard;
import com.ruoyi.project.jiaqin.downstandard.mapper.DownStandardMapper;
import com.ruoyi.project.jiaqin.downtrend.domain.DownTrend;
import com.ruoyi.project.jiaqin.downtrend.mapper.DownTrendMapper;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 羽绒行情Service业务层处理
 *
 * @author admin
 * @date 2021-01-29
 */
@Service
public class DownQuotationServiceImpl implements IDownQuotationService
{
    @Autowired
    private DownQuotationMapper downQuotationMapper;
    @Autowired
    private DownClassMapper downClassMapper;
    @Autowired
    private DownMeasureMapper downMeasureMapper;
    @Autowired
    private DownStandardMapper downStandardMapper;
    @Autowired
    private DownTrendMapper downTrendMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private CompanyMapper companyMapper;


    /**
     * 查询羽绒行情
     *
     * @param id 羽绒行情ID
     * @return 羽绒行情
     */
    @Override
    public DownQuotation selectDownQuotationById(String id)
    {
        return downQuotationMapper.selectDownQuotationById(id);
    }

    /**
     * 查询羽绒行情列表
     *
     * @param downQuotation 羽绒行情
     * @return 羽绒行情
     */
    @Override
    public List<DownQuotation> selectDownQuotationList(DownQuotation downQuotation)
    {
        return downQuotationMapper.selectDownQuotationList(downQuotation);
    }


    public PageInfo<DownQuotation> selectDownQuotationListA(Integer pageNum,Integer pageSize,DownQuotation downQuotation)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<DownQuotation> list = downQuotationMapper.selectDownQuotationList(downQuotation);
        for (DownQuotation dq:list) {
            //查用户信息
            Member member = memberMapper.selectMemberById(dq.getMemberId());
            if(member != null){
                dq.setMemberName(member.getUsername());
                if(member.getIsCompany() == 1){
                    //根据用户ID查询企业名称
                    Company company = companyMapper.selectCompanyByMemberId(dq.getMemberId());
                    if (company != null) {
                        dq.setCompanyName(company.getCompanyName());
                    }
                }
            }
        }
        PageInfo<DownQuotation> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public DownQuotation selectDownQuotationByFourId(String memberId, String downClassId, String downMeasureId, String downStandardId) {
        return downQuotationMapper.selectDownQuotationByFourId(memberId,downClassId,downMeasureId,downStandardId);
    }

    /**
     * 新增羽绒行情
     *
     * @param downQuotation 羽绒行情
     * @return 结果
     */
    @Override
    public int insertDownQuotation(DownQuotation downQuotation)
    {
        downQuotation.setId(UUID.randomUUID().toString().replace("-",""));
        return downQuotationMapper.insertDownQuotation(downQuotation);
    }

    /**
     * 修改羽绒行情
     *
     * @param downQuotation 羽绒行情
     * @return 结果
     */
    @Override
    public int updateDownQuotation(DownQuotation downQuotation)
    {
        return downQuotationMapper.updateDownQuotation(downQuotation);
    }

    /**
     * 删除羽绒行情对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDownQuotationByIds(String ids)
    {
        return downQuotationMapper.deleteDownQuotationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除羽绒行情信息
     *
     * @param id 羽绒行情ID
     * @return 结果
     */
    @Override
    public int deleteDownQuotationById(String id)
    {
        return downQuotationMapper.deleteDownQuotationById(id);
    }
//    public int downAvgStatistic(){
//        //声明一个List记录要插入数据库趋势表中的记录
//        List<DownTrend> list = new ArrayList<>();
//        //获取全部羽绒分类列表
//        List<DownClass> downClasses = downClassMapper.selectDownClassList(new DownClass());
//        //获取羽绒标准列
//        List<DownStandard> downStandards = downStandardMapper.selectDownStandardList(new DownStandard());
//        List<DownMeasure> downMeasures = downMeasureMapper.selectDownMeasureList(new DownMeasure());
//for(int q = 1 ;q<=7 ;q++){
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
//        calendar.add(Calendar.DATE,-q);
//        Date date = calendar.getTime();
//    int j = 10;
//        for (DownClass dc:downClasses) {
//            for (DownStandard ds:downStandards) {
//
////                List<DownTrend> dts = downTrendMapper.selectAvgPrice(dc.getId(),ds.getId());
////                for (DownTrend dt: dts) {
////                    dt.setId(UUID.randomUUID().toString().replace("-",""));
////                }
////                System.out.println(list.addAll(dts));
//                int i = 40;
//                //插入数据
//                for (DownMeasure dm :downMeasures) {//循环7次
//                    DownQuotation downQuotation= new DownQuotation();
//                    downQuotation.setDownMeasureId(dm.getId());
//                    downQuotation.setDownStandardId(ds.getId());
//                    downQuotation.setDownClassId(dc.getId());
//                    downQuotation.setDownClassName(dc.getName());
//                    downQuotation.setDownMeasureName(dm.getName());
//                    downQuotation.setDownStandardName(ds.getName());
//                    downQuotation.setReleaseDate(date);
//                    downQuotation.setMemberId("99d736bfb9154f5f84085510a80500ce");
//                    downQuotation.setPrice(new BigDecimal(600 -i-j));
//                    downQuotation.setProvince("16");
//                    downQuotation.setCity("183");
//                    downQuotation.setArea("1719");
//                    downQuotation.setMarketDynamic("羽绒不错");
//                    downQuotation.setReleaseArea("河南省郑州市金水区");
//                    insertDownQuotation(downQuotation);
//                    downQuotation.setPrice(new BigDecimal(580-i-j));
//                    insertDownQuotation(downQuotation);
//                    i = i+40;
//                }
//
//                j = j+10;
//            }
//        }
//}
//        return 0;
////        return  downTrendMapper.batchDownTrend(list);
//    }


    public int downAvgStatistic(){
        //声明一个List记录要插入数据库趋势表中的记录
        List<DownTrend> list = new ArrayList<>();
        //获取全部羽绒分类列表
        List<DownClass> downClasses = downClassMapper.selectDownClassList(new DownClass());
        //获取羽绒标准列
        List<DownStandard> downStandards = downStandardMapper.selectDownStandardList(new DownStandard());
        List<DownMeasure> downMeasures = downMeasureMapper.selectDownMeasureList(new DownMeasure());

        for (DownClass dc:downClasses) {
            for (DownStandard ds:downStandards) {
//                for(int q = 1;q<=7;q++){
                    List<DownTrend> dts = downTrendMapper.selectAvgPrice(dc.getId(),ds.getId(),1);
                    for (DownTrend dt: dts) {
                        dt.setId(UUID.randomUUID().toString().replace("-",""));
                    }
                    System.out.println(list.addAll(dts));
//                }
            }
        }
        return  downTrendMapper.batchDownTrend(list);
    }

}
